import { ref, computed, nextTick } from 'vue'
import { defineStore } from 'pinia'
import Cookie from 'js-cookie'
export const useMenuStore = defineStore({
  id:'menuState',
  state: () => ({
    state:null,
    }
  ),
  actions:{
    Ustate(){
      const menuState = JSON.parse(localStorage.getItem('menuState'))
      if(menuState == null){
        this.state = false
        localStorage.setItem('menuState',JSON.stringify(this.state))
      }else{
        this.state = !menuState
        localStorage.setItem('menuState',JSON.stringify(this.state))
      }
    },
    getState(){
      const menuState = JSON.parse(localStorage.getItem('menuState'))
      this.state = this.state || menuState
    }
  }
})

export const SelectMenu = defineStore({
  id: 'SelectMenu',
  state: () => ({
    currentMenu: null,
    tabList:[
      {
        path:'/',
        name:'home',
        label:'首页',
        icon:'home'
      }
    ],
    menu:[],
    token:'',
    user:{
      username:'',
      role:''
    }
  }),
  actions: {
    selectMenu(val) {
      //判断
    if(val.name == 'home'){
      this.currentMenu = null
      localStorage.removeItem('currentMenu')
    }else{
      console.log(val)
      this.currentMenu = val
      localStorage.setItem('currentMenu',JSON.stringify(this.currentMenu))
      let result = this.tabList.findIndex(item =>item.name === val.name)
      console.log(result)
      result == -1 ? this.tabList.push(val) :''
      localStorage.setItem('tabList',JSON.stringify(this.tabList))
      // this.selectedTab = val;
      // this.getToken()
    }
  },
    closeTab(val){
      let res = this.tabList.findIndex(item => item.name === val.name)
      this.tabList.splice(res,1)
      localStorage.setItem('tabList',JSON.stringify(this.tabList))

      // this.getToken()
    },
    setMenu(val){
      console.log(val)
      this.menu = val
      localStorage.setItem('menu',JSON.stringify(val))
    },
    addMenu(router){
      if(!localStorage.getItem('menu')){
        return
      }
      const menu = JSON.parse(localStorage.getItem('menu'))
      this.menu = menu
      
      const menuArray = []

      this.menu.forEach(item=>{
        if(item.children){
          item.children = item.children.map(item=>{
            let url = `../views/${item.url}.vue`
            item.component = ()=>import(/* @vite-ignore */ url)
            return item
          })
          menuArray.push(...item.children)
        }else{
          let url = `../views/${item.url}.vue`
          item.component = ()=>import(/* @vite-ignore */ url)
          menuArray.push(item)
        }
      })
      menuArray.forEach(item=>{
        router.addRoute('home1',item)
      })
    },
    clearMenu(){
      this.menu = ''
      localStorage.removeItem('menu')
      localStorage.removeItem('tabList')
    },
    setToken(val){
      this.token = val
      Cookie.set('token',val)
    },
    clearToken(){
      this.token = ''
      Cookie.remove('token')
    },
    getToken(){
      this.token = this.token ||Cookie.get('token')
      // const savedTabList = localStorage.getItem('tabList');
      // if (savedTabList) {
      //   this.tabList = JSON.parse(savedTabList);
      //   this.selectedTab = this.tabList.find(item => item.name === this.currentMenu?.name);
      // }
    },
    setUser(user){
      this.user.username = user.userName
      this.user.role = user.roleId
      localStorage.setItem("user",JSON.stringify(this.user))
    },
    clearUser(){
      this.token = ''
      localStorage.removeItem("user")
    },
    getUser(){
      console.log()
      this.user = JSON.parse(localStorage.getItem('user'))
    },
    changeCurrentMenu(){
      this.currentMenu = this.currentMenu || JSON.parse(localStorage.getItem('currentMenu'))
    }
  },
});




